/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkg16b_eca;

import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import pkg16b_eca.data.validate.Person;
import pkg16b_eca.data.validate.Text;
import javax.swing.BorderFactory;
import pkg16b_eca.SQL.Tables.SQLSkadespelare;
import se.oru.informatik.InformatikException;
import pkg16b_eca.SQL.SQLCollection;

/**
 *
 * @author Tabascomang
 */
public class SkadespelareProfil extends javax.swing.JFrame {
    public String TempNamn;
    public String TempRoll;
    private final SQLCollection SQL;
    /**
     * Creates new form SkadespelareProfil
     */
    public SkadespelareProfil() throws InformatikException {
        initComponents();
        SQL = new SQLCollection();
        
    }
    // width 594 height 360
    // [2.2.2]
     public void Show(String name, String roll) { //This method shows the SkadespelareProfil JFrame,
        this.setVisible(true);       //And also fills in all it's components with information about
                                     //the choosen actor from the third step in Sök och matcha "Matcha".
        SkadespelareProfilBorder.setBorder(BorderFactory.createLineBorder(Color.black));
        //Sets a black small border around the JPanel that is exactly the samer size as this JFrame.
            
            TempNamn = name;
            TempRoll = roll;
            
        
        
        //Kod för att visa skådespelare.
        HashMap<String, String> rvn = null; //Read comments in the block between 119 and 130. Only difference is that we do not use ArrayList here.
        try {
            rvn = new SQLSkadespelare().getAttribut(name);
        } catch (InformatikException ex) {
            Logger.getLogger(ECA.class.getName()).log(Level.SEVERE, null, ex);
        }
        //This method is used to insert all the attributes belonging to a
        //actor, besides the SID and AID, into the txtAreaSkadespelareAttributProfil
        //component in ECA.java. You can see here below what is done.

        String thisname = rvn.get("namn"); //grab the value and insert it into a string for later use
        String thiskon = Person.getKon(Text.parseNumber(rvn.get("kon"))).toString();
        String thisalder = rvn.get("alder");
        String thisvikt = rvn.get("vikt");
        String thislangd = rvn.get("langd");
        String thisharfarg = rvn.get("harfarg");
        String thisogonfarg = rvn.get("ogonfarg");

        txtAreaSkadespelareAttributProfil.setText( //we reassign the thiskon variable with a string instead.
                "Namn: " + thisname + "\n" +  //in this first if clause. we say "Man".
                "Kön: " + thiskon + "\n" +
                "Ålder: " + thisalder + "\n" + 
                "Vikt: " + thisvikt + "\n" +
                "Längd(cm): " + thislangd + "\n" +
                "Hårfärg: " + thisharfarg + "\n" +
                "Ögonfärg: " + thisogonfarg);
        
        rvn.clear(); // [2.2.2]
//-----------------------------------------------------------------------------------------------------------------
        ArrayList<String> formogor = null; //Read comments in the block between 119 and 130. Only difference is that we do not use HashMap here.
        try {
            formogor = new SQLSkadespelare().getFormagorNamn(name);
        } catch (InformatikException ex) {
            Logger.getLogger(SkadespelareProfil.class.getName()).log(Level.SEVERE, null, ex);
        }
            //Call the getSkadespelare method, and the underlying method getFormagorNamn.
            //We insert the name that was selected from the result list, that will cause the methods to return
            //all the abilities (Formagor) of a certain actor, and insert then into a Arraylist called rvn.
            if (formogor != null && !formogor.isEmpty()) { //If rvn is not null, nor empty. Do this:
                for (String s : formogor) { //For each value in null, take it to the string s, and add
                    lstSkadespelareFormagorProfil.add(s); //it to the list.
                }
    }
            if (formogor != null && formogor.size() > 0) { // [2.2.2]
                formogor.clear();
            }
            
 //----------------------------------------------------------------------------------------------------------------
            ArrayList<HashMap<String, String>> produktioner = null; //Read comments in the block between 119 and 130
        try {            
            produktioner = new SQLSkadespelare().getProduktioner(name);
        } catch (InformatikException ex) {
            Logger.getLogger(SkadespelareProfil.class.getName()).log(Level.SEVERE, null, ex);
        }
            
            for (HashMap<String, String> s : produktioner) {
             lstSkadespelareProduktionerProfil.add(s.get("titel"));
             
         
     }
            if (produktioner != null && produktioner.size() > 0) { // [2.2.2]
                produktioner.clear();
            }
            
//-------------------------------------------------------------------------------------------------------------------
       ArrayList<String> tidigareroller = null; //Read comments in the block between 119 and 130. Only difference is that we do not use HashMap here.
        try {
            tidigareroller = new SQLSkadespelare().getRoller(name);
        } catch (InformatikException ex) {
            Logger.getLogger(SkadespelareProfil.class.getName()).log(Level.SEVERE, null, ex);
        }
            //Call the getSkadespelare method, and the underlying method getFormagorNamn.
            //We insert the name that was selected from the result list, that will cause the methods to return
            //all the abilities (Formagor) of a certain actor, and insert then into a Arraylist called rvn.
            if (tidigareroller != null && !tidigareroller.isEmpty()) { //If rvn is not null, nor empty. Do this:
                for (String s : tidigareroller) { //For each value in null, take it to the string s, and add
                    lstSkadespelareRollerProfil.add(s); //it to the list
                }
    }
            if (tidigareroller != null && tidigareroller.size() > 0) { // [2.2.2]
                tidigareroller.clear();
            }
            
//--------------------------------------------------------------------------------------------------------------------
            ArrayList<HashMap<String, String>> foreslagnaroller = null; //Delcare a ArrayList with Hashmap String,String
        try { //When trying to use getSkadespelare() like you did in ECAFunctions, you instead need to sorround it with a 
            foreslagnaroller = new SQLSkadespelare().getForslagenRoll(name); // try-catch block, and use SQLSkadespelare()
        } catch (InformatikException ex) {                                   //instead. Assign it to foreslagnaroller (or your choosen name for the Arraylist.)
            Logger.getLogger(SkadespelareProfil.class.getName()).log(Level.SEVERE, null, ex);
        }
        for (HashMap<String, String> s : foreslagnaroller) {
            
            lstSkadespelareTidigareForeslagnaRollerProfil.add(s.get("rnamn"));
        }
        if (foreslagnaroller != null && foreslagnaroller.size() > 0) { // [2.2.2]
            foreslagnaroller.clear();
        }
        
//----------------------------------------------------------------------------------------------------------------------
     
     
     
     }
    
    public void Hide() {
        txtAreaSkadespelareAttributProfil.removeAll();
        lstSkadespelareFormagorProfil.removeAll();
        lstSkadespelareProduktionerProfil.removeAll();
        lstSkadespelareRollerProfil.removeAll();
        lstSkadespelareTidigareForeslagnaRollerProfil.removeAll();
        
        
        this.setVisible(false);
        //This method clears all the lists of the data, before hiding the JFrame SkadespelareProfil.
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        SkadespelareProfilBorder = new javax.swing.JPanel();
        lstSkadespelareProduktionerProfil = new java.awt.List();
        lstSkadespelareFormagorProfil = new java.awt.List();
        lstSkadespelareRollerProfil = new java.awt.List();
        lstSkadespelareTidigareForeslagnaRollerProfil = new java.awt.List();
        btnCloseSkadespelareProfilForm = new javax.swing.JButton();
        label1 = new java.awt.Label();
        label2 = new java.awt.Label();
        label3 = new java.awt.Label();
        label4 = new java.awt.Label();
        label5 = new java.awt.Label();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtAreaSkadespelareAttributProfil = new javax.swing.JTextArea();
        btnLaggTillSkadespelareTillRollenSkadespelareProfil = new javax.swing.JButton();

        setAlwaysOnTop(true);
        setMaximumSize(new java.awt.Dimension(594, 360));
        setMinimumSize(new java.awt.Dimension(594, 360));
        setUndecorated(true);
        setPreferredSize(new java.awt.Dimension(594, 360));
        setResizable(false);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });

        SkadespelareProfilBorder.setMaximumSize(new java.awt.Dimension(594, 360));
        SkadespelareProfilBorder.setMinimumSize(new java.awt.Dimension(594, 360));
        SkadespelareProfilBorder.setPreferredSize(new java.awt.Dimension(579, 360));

        btnCloseSkadespelareProfilForm.setFont(new java.awt.Font("Tahoma", 1, 48)); // NOI18N
        btnCloseSkadespelareProfilForm.setText("X");
        btnCloseSkadespelareProfilForm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseSkadespelareProfilFormActionPerformed(evt);
            }
        });

        label1.setText("Har innehaft dessa roller");

        label2.setText("Profil");

        label3.setText("Blivit föreslagen för dessa roller");

        label4.setText("Tidigare produktioner");

        label5.setText("Har dessa förmågor");

        txtAreaSkadespelareAttributProfil.setColumns(20);
        txtAreaSkadespelareAttributProfil.setRows(5);
        txtAreaSkadespelareAttributProfil.setMaximumSize(new java.awt.Dimension(192, 94));
        txtAreaSkadespelareAttributProfil.setMinimumSize(new java.awt.Dimension(192, 94));
        txtAreaSkadespelareAttributProfil.setOpaque(false);
        txtAreaSkadespelareAttributProfil.setPreferredSize(new java.awt.Dimension(192, 94));
        jScrollPane1.setViewportView(txtAreaSkadespelareAttributProfil);

        btnLaggTillSkadespelareTillRollenSkadespelareProfil.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnLaggTillSkadespelareTillRollenSkadespelareProfil.setText("Lägg skådespelare till rollen");
        btnLaggTillSkadespelareTillRollenSkadespelareProfil.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLaggTillSkadespelareTillRollenSkadespelareProfilActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout SkadespelareProfilBorderLayout = new javax.swing.GroupLayout(SkadespelareProfilBorder);
        SkadespelareProfilBorder.setLayout(SkadespelareProfilBorderLayout);
        SkadespelareProfilBorderLayout.setHorizontalGroup(
            SkadespelareProfilBorderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(SkadespelareProfilBorderLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(SkadespelareProfilBorderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lstSkadespelareRollerProfil, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lstSkadespelareProduktionerProfil, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(label4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(label1, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(SkadespelareProfilBorderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(SkadespelareProfilBorderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(lstSkadespelareFormagorProfil, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label5, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(SkadespelareProfilBorderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lstSkadespelareTidigareForeslagnaRollerProfil, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(label3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnCloseSkadespelareProfilForm, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnLaggTillSkadespelareTillRollenSkadespelareProfil, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(48, 48, 48))
        );
        SkadespelareProfilBorderLayout.setVerticalGroup(
            SkadespelareProfilBorderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(SkadespelareProfilBorderLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(SkadespelareProfilBorderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(SkadespelareProfilBorderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                    .addComponent(lstSkadespelareRollerProfil, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lstSkadespelareTidigareForeslagnaRollerProfil, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(SkadespelareProfilBorderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(SkadespelareProfilBorderLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(SkadespelareProfilBorderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(label5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(label4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(SkadespelareProfilBorderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lstSkadespelareProduktionerProfil, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lstSkadespelareFormagorProfil, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(SkadespelareProfilBorderLayout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(btnCloseSkadespelareProfilForm)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnLaggTillSkadespelareTillRollenSkadespelareProfil, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(SkadespelareProfilBorder, javax.swing.GroupLayout.DEFAULT_SIZE, 619, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(SkadespelareProfilBorder, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCloseSkadespelareProfilFormActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseSkadespelareProfilFormActionPerformed
        Hide();
        //Event ActionPeformed (you clicked the button), run the code above this comment.
    }//GEN-LAST:event_btnCloseSkadespelareProfilFormActionPerformed

    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
        invalidate();
        validate();
        //Attempt to fix graphical glitches (lagg, missing graphics) that may appear sometimes.
    }//GEN-LAST:event_formComponentResized

    private void btnLaggTillSkadespelareTillRollenSkadespelareProfilActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLaggTillSkadespelareTillRollenSkadespelareProfilActionPerformed
        // [2.2.1]
        System.out.println(TempRoll);
        int _RID = SQL.getRoll().getRollRID(TempRoll);
        int _SID = SQL.getSkadespelare().getSkadespelareSID(TempNamn);
        System.out.println(_RID);
        System.out.println(_SID);
        
        SQL.getRoll().replaceSkadespelare(_RID, _SID);
        this.setVisible(false);
        TempRoll = null;
        TempNamn = null;
        txtAreaSkadespelareAttributProfil.removeAll();
        lstSkadespelareFormagorProfil.removeAll();
        lstSkadespelareProduktionerProfil.removeAll();
        lstSkadespelareRollerProfil.removeAll();
        lstSkadespelareTidigareForeslagnaRollerProfil.removeAll();
        
    }//GEN-LAST:event_btnLaggTillSkadespelareTillRollenSkadespelareProfilActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SkadespelareProfil.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SkadespelareProfil.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SkadespelareProfil.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SkadespelareProfil.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new SkadespelareProfil().setVisible(true);
                } catch (InformatikException ex) {
                    Logger.getLogger(SkadespelareProfil.class.getName()).log(Level.SEVERE, null, ex);
                }
                
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    protected javax.swing.JPanel SkadespelareProfilBorder;
    private javax.swing.JButton btnCloseSkadespelareProfilForm;
    private javax.swing.JButton btnLaggTillSkadespelareTillRollenSkadespelareProfil;
    private javax.swing.JScrollPane jScrollPane1;
    private java.awt.Label label1;
    private java.awt.Label label2;
    private java.awt.Label label3;
    private java.awt.Label label4;
    private java.awt.Label label5;
    protected java.awt.List lstSkadespelareFormagorProfil;
    protected java.awt.List lstSkadespelareProduktionerProfil;
    protected java.awt.List lstSkadespelareRollerProfil;
    protected java.awt.List lstSkadespelareTidigareForeslagnaRollerProfil;
    protected javax.swing.JTextArea txtAreaSkadespelareAttributProfil;
    // End of variables declaration//GEN-END:variables
}
